package com.lkd.client.service.impl;

import com.alibaba.fastjson.JSON;
import com.lkd.client.config.EmqConfig;
import com.lkd.client.emq.EmqClient;
import com.lkd.client.emq.msg.*;
import com.lkd.client.service.DataProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional //所有方法支持事物回滚
public class DataProcessServcieIml implements DataProcessService {

    @Autowired
    private EmqClient emqClient;

    @Autowired
    private EmqConfig config;

    @Autowired
    private PhotoRespData photoRespData;



    @Override
    public void photoReq(PhotoReq photoReq) {
        if (photoReq.getPhotoReqData()!=null){
            System.out.println("拍照请求处理中，正在拍照");
            //假设拍照成功：
            photoRespData.setIfSucceed(true);
            photoResp();
        }else {
            log.error("拍照请求错误！");
        }
    }

    @Override
    public void photoResp() {
        //判断是否执行拍照命令成功！
        if (photoRespData.isIfSucceed()) {
            PhotoResp photoResp = new PhotoResp();
            photoResp.setInnerCode(config.getInnerCode());
            photoResp.setNeedResp(false);
            photoResp.setSn(System.nanoTime());
            emqClient.publish(photoResp.getMsgType(), JSON.toJSONString(photoResp));//    toServer/1/photoResp
        }
    }

    @Override
    public void linResponse(SanlinReqData linResponse) {

        emqClient.publish(JSON.toJSONString(linResponse));//    toServer/1/photoResp

    }

}
